package com.lcc.ioc;

import com.lcc.ioc.demos.web.component.UserComponent;
import com.lcc.ioc.demos.web.configuration.UserConfiguration;
import com.lcc.ioc.demos.web.configuration.UserInfo;
import com.lcc.ioc.demos.web.controller.UserController;
import com.lcc.ioc.demos.web.repo.UserRepository;
import com.lcc.ioc.demos.web.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class IocDemoApplication {

    public static void main(String[] args) {
        //这里就是将Spring应用启动起来，启动起来会返回Spring的容器,采用ApplicationContext来接收，返回的就是Spring的运行环境
        // 那存放再里面的对象从这个容器里面去拿就可以了
        //ApplicationContext称为Spring上下文，就是应用环境，其实就是Spring的运行环境
        ApplicationContext context = SpringApplication.run(IocDemoApplication.class, args);
        //通过对象的类型去拿对象
        UserController bean = context.getBean(UserController.class);
        //验证是否真的拿到了UserController这个对象
        bean.doController();
//        UserService userService = context.getBean(UserService.class);
//        userService.doService();
//        //根据名称来获取bean,返回的是一个Object类型。同时许如需要调用该类的方法就需要做类型的转换
//        UserController userController = (UserController)context.getBean("userController");
//        userController.doController();
//        //根据名称和类型获取bean
//        UserService userService1 = context.getBean("userService",UserService.class);
//        userService1.doService();
//        UserRepository userRepository = context.getBean(UserRepository.class);
//        userRepository.doRepository();
//        UserComponent userComponent = context.getBean(UserComponent.class);
//        userComponent.doComponent();
//        UserConfiguration userConfiguration = context.getBean(UserConfiguration.class);
//        userConfiguration.doConfiguration();
//        UserConfiguration userConfiguration1 = context.getBean(UserConfiguration.class);
//        userConfiguration1.doConfiguration();
//        //可以验证两次获取到的对象是一个对象
//        System.out.println("userConfiguration==userConfiguration1 :"  + (userConfiguration==userConfiguration1));
//        //通过方法注解@Bean获取到两个同一类型的不同对象
//        UserInfo userInfo = (UserInfo)context.getBean("userInfo");//直接使用名称获取对象需要进行强制类型转换
//        System.out.println(userInfo);
//        //使用方法注解@Bean，此时的bean name，也就使对象名字是方法的名字
//        UserInfo userInfo1 = context.getBean("userInfo2",UserInfo.class);
//        System.out.println(userInfo1);
    }

}
